package com.zxy.springbootactivemq;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jms.connection.JmsTransactionManager;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.transaction.PlatformTransactionManager;

import javax.jms.ConnectionFactory;
import javax.jms.DeliveryMode;

/**
 * @author zhangxuyuan
 * @version 1.0
 * @date 2020/11/22
 */
@Configuration
public class ActiveMQConfig {

    @Bean
    public JmsTemplate asynJmsTemplate(ConnectionFactory connectionFactory)
    {
        JmsTemplate template = new JmsTemplate(connectionFactory);
        /**
         * 可以设置持久化，延迟投递，投递优先级等
         * 通过template.setXXX()系列方法
         */
        template.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
        return template;
    }

    /**
     * 启动事务支持
     * @param connectionFactory
     * @return
     */
    @Bean
    public PlatformTransactionManager platformTransactionManager(ConnectionFactory connectionFactory)
    {
        return new JmsTransactionManager(connectionFactory);
    }
}
